﻿using ELTE.SEM.Common.Enums;
using ELTE.SEM.Data.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ELTE.SEM.Data
{
    public static class UserEx
    {
        public static UserRoleLevel GetUserRoleLevel(this User user)
        {
            if (user.UsersInRoles.Count > 0)
            {
                UserRole role = user.UsersInRoles.SingleOrDefault(s => s.UserId == user.Id).UserRole;
                if (role != null)
                {
                    return (UserRoleLevel)role.Id;
                }
                else
                {
                    return UserRoleLevel.Unknown;
                }
            }
            else
            {
                return UserRoleLevel.Unknown;
            }
        }

        public static UserRoleViewModel GetUserRoleViewModel(this User user)
        {
            if (user.UsersInRoles.Count > 0)
            {
                UserRole role = user.UsersInRoles.SingleOrDefault(s => s.UserId == user.Id).UserRole;
                if (role != null)
                {
                    return new UserRoleViewModel { RoleId = role.Id, RoleName = role.RoleDisplayName };
                }
                else
                {
                    return new UserRoleViewModel { RoleId = 0, RoleName = "Ismeretlen" };
                }
            }
            else
            {
                return new UserRoleViewModel { RoleId = 0, RoleName = "Ismeretlen" };
            }
        }
    }
}
